using System;
using System.Collections.Generic;
using System.Text;

namespace LocalizationTest
{
  class ProbabilityDensityFunction
  {
    double _minimumX;
    double _maximumX;
    double[] _values;

    public ProbabilityDensityFunction(double[] values, double minimumX, double maximumX)
    {
      _values = (double[])values.Clone();
      _minimumX = minimumX;
      _maximumX = maximumX;
    }

    public ProbabilityDensityFunction(int numberOfValues, double minimumX, double maximumX)
    {
      _values = new double[numberOfValues];
      _minimumX = minimumX;
      _maximumX = maximumX;
    }

    public bool AddValue(double x, double value)
    {
      int index = (int)((x - _minimumX) * _values.Length / (_maximumX - _minimumX));
      if (index >= 0 && index < _values.Length)
      {
        _values[index] = value;
        return true;
      }
      else
      {
        return false;
      }
    }

    public double GetValue(double x)
    {
      int index = (int)((x - _minimumX) * _values.Length / (_maximumX - _minimumX));
      if (index >= 0 && index < _values.Length)
      {
        return _values[index];
      }
      else
      {
        return 0;
      }
    }

    public double GetNumberOfValues()
    {
      return _values.Length;
    }

    public void Normalize()
    {
      double sum = 0;
      for (int x = 0; x < _values.Length; x++)
      {
        sum += _values[x];
      }
      for (int x = 0; x < _values.Length; x++)
      {
        if (sum == 0)
        {
          _values[x] = 1.0 / _values.Length;
        }
        else _values[x] /= sum;
      }

    }

    public void InitWithEqualDistribution()
    {
      for (int x = 0; x < _values.Length; x++)
      {
        _values[x] = 1.0 / _values.Length;
      }
    }

    public ProbabilityDensityFunction Copy()
    {
      return new ProbabilityDensityFunction(_values, _minimumX, _maximumX);
    }


  }
}
